library(ggeffects)
#Theme
Theme = theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.title=element_text(size=20),
axis.text=element_text(size=18),strip.text = element_text(size = 16),legend.text = element_text(size=14),legend.title = element_text(size=16))
# Load Data ---------------------------------------------------------------
Fin = read_csv("Processed Data/Final_Data.csv")
table(Fin$System2)
#Remove replacement scales
Fin=subset(Fin, is.na(Check))
# VDA for growth and metabolism -------------------------------------------
table(Fin$System2,Fin$SP,Fin$Migrant)
#make factors
Fin$SP=as.factor(Fin$SP)
Fin$System2=as.factor(Fin$System2)
Fin$Day_un=as.factor(Fin$Day_un)
Fin$Run_un=as.factor(Fin$Run_un)
96+148+182+252+72
#Remove key NA's
Fin <- Fin%>%
drop_na(MR_M,MR_MT, G_day_S, Temp, Age, Condition,System,SP,mean_W_temp)
#River temp
Fin%>%
group_by(System2)%>%
summarise(mean(Temp),length(System2))
Fin%>%
group_by(Migrant,System2)%>%
summarise(length(System2))
sum(171+187+185+72+177+249)
table(Fin$System2, Fin$Site)
16+34+34+23+32+32
table(Fin$System2)
library(tidyverse)
library(readxl)
library(respR)
library(dplyr)
library(nlme)
library(lme4)
library(performance)
library(MuMIn)
library(corrplot)
library(DHARMa)
library(glmmTMB)
library(cowplot)
library(ggplot2)
library(car)
Theme = theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.title=element_text(size=20),
axis.text=element_text(size=18),strip.text = element_text(size = 16),legend.text = element_text(size=14),legend.title = element_text(size=16))
# Load Data ---------------------------------------------------------------
Fin = read_csv("Processed Data/Final_Data.csv")
table(Fin$System2)
#Same data removal as before
sum(!is.na(Fin$tag))
Fin=subset(Fin, is.na(Check))
#Install Libraries
library(tidyverse)
library(ggplot2)
library(readxl)
library(mgcv)
library(dplyr)
library(visreg)
library(mgcViz)
library(mapdata)
library(viridis)
library(forcats)
library(RColorBrewer)
Theme = theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.title=element_text(size=24),axis.text=element_text(size=22),legend.text = element_text(size=14),legend.title = element_text(size=16))
xlim <- c(-30, 20)#Longitudinal limits
ylim <- c(34, 71)#Latitudinal limits
m1 <- map_data("worldHires")
Map <- ggplot() +
theme_bw() +
geom_polygon(
data = m1,
aes(long, lat, group = group),
fill = "#878787"
) +
coord_quickmap(
xlim = xlim,
ylim = ylim,
expand = FALSE
)
library(ggplot2)
p <- ggplot()
lat <- c(
"43.210670",
"65.67710",
"50.684807",
"57.369391",
"55.58"
)
lon <- c(
"-4.813315",
"-14.951928",
"-2.225094",
"-3.020355",
"-2.68"
)
Site <- c(
"Cares",
"Vesturdalsá\nand Sunnudalsá",
"Frome",
"Deveron",
"Tweed"
)
Plot <- data.frame(Site, lat, lon)
Plot$lat <- as.numeric(Plot$lat)
Plot$lon <- as.numeric(Plot$lon)
site_order <- c(
"Vesturdalsá\nand Sunnudalsá",
"Deveron",
"Cares",
"Tweed",
"Frome"
)
site_order <- c(
"Vesturdalsá\nand Sunnudalsá",
"Deveron",
"Tweed",
"Cares",
"Frome"
)
Plot$Site <- factor(Plot$Site, levels = site_order)
library(paletteer)
# Original colours retained for each site
site_cols <- c(
"Vesturdalsá\nand Sunnudalsá" = "#3A488AFF",
"Deveron" = "#9A98C0FF",
"Tweed" = "#DABD61FF",
"Cares" = "#D95F30FF",
"Frome" = "#BE3428FF"
)
MapR = Map +
ylab("Latitude") +
xlab("Longitude") +
Theme +
geom_point(
data = Plot,
aes(x = lon, y = lat, color = Site),
size = 5
) +
scale_color_manual(name="River",
values = site_cols,
breaks = site_order
)+ theme(
legend.position = c(0.02, 0.02),
legend.justification = c(0, 0)
)
MapR
ggsave("Plots/Site map.tiff", width = 8, height = 8, units = "in", dpi = 200)
library(tidyverse)
library(readxl)
library(respR)
library(dplyr)
library(nlme)
library(lme4)
library(performance)
library(MuMIn)
library(corrplot)
library(viridis)
library(cowplot)
library(paletteer)
library(ggeffects)
#Theme
Theme = theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.title=element_text(size=20),
axis.text=element_text(size=18),strip.text = element_text(size = 16),legend.text = element_text(size=14),legend.title = element_text(size=16))
# Load Data ---------------------------------------------------------------
Fin = read_csv("Processed Data/Final_Data.csv")
table(Fin$System2)
#Remove replacement scales
Fin=subset(Fin, is.na(Check))
# VDA for growth and metabolism -------------------------------------------
table(Fin$System2,Fin$SP,Fin$Migrant)
#make factors
Fin$SP=as.factor(Fin$SP)
Fin$System2=as.factor(Fin$System2)
Fin$Day_un=as.factor(Fin$Day_un)
Fin$Run_un=as.factor(Fin$Run_un)
96+148+182+252+72
#Remove key NA's
Fin <- Fin%>%
drop_na(MR_M,MR_MT, G_day_S, Temp, Age, Condition,System,SP,mean_W_temp)
#River temp
Fin%>%
group_by(System2)%>%
summarise(mean(Temp),length(System2))
Fin%>%
group_by(Migrant,System2)%>%
summarise(length(System2))
sum(171+187+185+72+177+249)
table(Fin$System2, Fin$Site)
#Start Basic
m1=lmer(log(MR_M)~Temp+System2+(1|System2:Day_un/Run_un), Fin, na.action = "na.fail")#Best model ffollowing model simplification
summary(m1)
library(lmerTest)
summary(m1, ddf = "Kenward-Roger")
summary(m1)
summary(m1, ddf = "Kenward-Roger")
glht(m1)
library(car)
Anova(m1, type = 2)
library(emmeans)
emm <- emmeans(m1, ~ System2)
pairs(emm, adjust = "tukey")
##PLOT
#Shows metabolism varies between sites and with temperature but no interaction, lat makes no change
river_levels <- c(
"Sunnudalsá",
"Vesturdalsá",
"Deveron",
"Tweed",
"Cares",
"Frome"
)
river_cols <- c(
"Sunnudalsá" = "#04111D",
"Vesturdalsá" = "#445394",
"Deveron" = "#9A98C0FF",
"Tweed" = "#DABD61FF",
"Cares" = "#D95F30FF",
"Frome" = "#BE3428FF"
)
rename_rivers <- c(
"Vesta" = "Vesturdalsá",
"Sunnudalsa" = "Sunnudalsá",
"Cantabria" = "Cares",
"Frome2" = "Frome"
)
Fin <- Fin %>%
mutate(
`River system` = dplyr::recode(System2, !!!rename_rivers),
`River system` = factor(`River system`, levels = river_levels)
)
meta = data.frame(
System2 = c("Cantabria", "Frome2", "Tweed", "Deveron", "Sunnudalsa", "Vesta"),
Air_temp = c(10.5, 10.3, 8.1, 7.7, 1.3, 1.3),
NDVI = c(0.638, 0.724, 0.664, 0.603, 0.240, 0.238)
)#Add water temp
Fin=merge(Fin, meta, by=c("System2"))
# Growth Modelling --------------------------------------------------------
Fin$`River system`=Fin$System2
Fin <- Fin %>%
mutate(`River system` = if_else(`River system` == "Frome2", "Frome", `River system`))%>%
mutate(`River system` = if_else(`River system` == "Cantabria", "Cares", `River system`))%>%
mutate(`River system` = if_else(`River system` == "Vesta", "Vesturdalsá", `River system`))%>%
mutate(`River system` = if_else(`River system` == "Sunnudalsa", "Sunnudalsá", `River system`))
Fin <- Fin %>%
mutate(Age2 = paste0(as.character(Age), "+"))
Fin$`River system` <- factor(
Fin$`River system`,
levels = c("Frome",  "Cares","Tweed", "Deveron", "Vesturdalsá", "Sunnudalsá")
)
#Final stepwise models below
m1 <- lmer(
G_day_S ~ log(MR_MT) * Annual_Mean_temp +SP*log(MR_MT)+
(1 | System2:Day_un/Run_un) +
(1 | System2:Age), data = Fin)#Remove non significant terms stepwise (This all terms significant)
summary(m1)
# 1. Residuals vs Fitted
plot(fitted_vals, resid_pear,
xlab = "Fitted values", ylab = "Normalized residuals",
main = "Residuals vs Fitted")
#My method
library(lme4); library(ggeffects); library(ggplot2)
Fin$G_REcorr <- predict(m1, re.form = NA) + resid(m1)
pred <- ggpredict(m1,
terms = c("MR_MT [all]",
"Annual_Mean_temp [minmax]"))
pred$Annual_Mean_temp <- c(min(Fin$Annual_Mean_temp),max(Fin$Annual_Mean_temp))
library(scales)
p1 = ggplot(
Fin,
aes(
log(MR_MT),
G_REcorr,
colour = Annual_Mean_temp))  +
geom_ribbon(
data = pred,
aes(
x = log(x),
ymin = conf.low,
ymax = conf.high,
fill = as.numeric(Annual_Mean_temp),
group = group),
alpha = 0.15,
colour = NA,
inherit.aes = FALSE) +
geom_line(
data = pred,
aes(x = log(x),
y = predicted,
colour = as.numeric(Annual_Mean_temp),
group = group),
linewidth = 1.1)+
geom_point(alpha = 0.5) +
scale_colour_gradientn(
colours = c(
"#04111D",
"#445394",
"#9A98C0FF",
"#DABD61FF",
"#D95F30FF",
"#BE3428FF"),
values = c(0, 0.1, 0.35, 0.45, 0.65, 0.9, 1),
name = "Mean river \ntemperature\n(°C)") +
scale_fill_gradientn(
colours = c(
"#04111D",
"#445394",
"#9A98C0FF",
"#DABD61FF",
"#D95F30FF",
"#BE3428FF"),
values = c(0, 0.1, 0.35, 0.45, 0.65, 0.9, 1),
guide = "none") +
theme_bw() +
#xlim(-2.5, 0) +
Theme +
labs(
x = "ln mass- and temperature-\ncorrected metabolism ",
y = expression("Growth (mm day"^{-1}*")"))+xlab(expression(paste("Ln metabolism", " (mg O"[2]," h"^-1," g"^-"0.86"," K"^-"0.36",")")))+
theme(legend.position = c(0.15, 0.98),legend.justification = c(1, 1),legend.background = element_rect(fill = NA, colour = NA),
legend.key = element_rect(fill = NA, colour = NA))
p1
table(Fin$`River system`,Fin$Annual_Mean_temp)
#Plot trout metabolism interaction
library(ggeffects)
pred_sp <- ggpredict(
m1,
terms = c("MR_MT [all]", "SP"),
condition = c(Annual_Mean_temp = mean(Fin$Annual_Mean_temp,
na.rm = TRUE))
)
p2_sup=ggplot() +
geom_point(data = Fin,
aes(log(MR_MT), G_REcorr, colour = SP),
alpha = 0.7) +
geom_line(data = pred_sp,
aes(log(x), predicted, colour = group),
linewidth = 1.3) +
geom_ribbon(data = pred_sp,
aes(log(x), ymin = conf.low,
ymax = conf.high, fill = group),
alpha = 0.3, colour = NA) +
labs(x = "log(Metabolism)",
y = "Growth",
colour = "Species",
fill = "Species") +
theme_bw()+Theme+xlim(-2.5,0)+
labs(
x = "ln(mass- and temperature-\ncorrected metabolism)",
y = "Growth (mm day⁻¹)") + ylab(expression("Growth (mm day"^{-1}*")")) +
theme(legend.position = c(0.98, 0.98),legend.justification = c(1, 1))+xlab(expression(paste("Ln metabolism", " (mg O"[2]," h"^-1," g"^-"0.86"," K"^-"0.36",")")))
p2_sup
#Look at just growth between rivers when correcting just for age effects
m1 <- lmer(
G_day_S ~ log(MR_MT) * Annual_Mean_temp +SP*log(MR_MT)+
(1 | System2:Day_un/Run_un) +
(1 | Age), data = Fin)#Remove non significant terms stepwise (This all terms significant)
summary(m1)
# Full conditional predictions
Fin$pred_allRE <- predict(m1)
# Predictions excluding System2:Age
Fin$pred_noAge <- predict(
m1,
re.form = ~(1 | System2:Day_un/Run_un)
)
# Remove only age random effects
Fin$G_agecorr <- Fin$G_day_S -
(Fin$pred_allRE - Fin$pred_noAge)
Fin$`River system` <- factor(Fin$`River system`,levels = c("Sunnudalsá","Vesturdalsá","Deveron","Tweed","Cares","Frome"))
p3=ggplot(Fin,
aes(`River system`, G_agecorr)) +
geom_boxplot(width = 0.15,
outlier.shape = NA) +geom_jitter(width=.03,alpha=0.3)+
theme_bw()+Theme + ylab(expression("Growth (mm day"^{-1}*")"))+labs(x="River")
#LM on this model
m5=lm(G_agecorr~System2, Fin)
summary(m5)
anova(m5)
r2(m5)
Fin%>%
group_by(System2)%>%
summarise(mean(G_agecorr))
library(emmeans)
emm <- emmeans(m5, ~ System2)
# Pairwise comparisons
pairs(emm)
#Plot together
library(cowplot)
plot_grid(
p1, p3,
ncol = 1,
rel_heights = c(1.5, 1),labels=c("a","b"),label_size = 16
)
p1 = ggplot(
Fin,
aes(
log(MR_MT),
G_REcorr,
colour = Annual_Mean_temp))  +
geom_ribbon(
data = pred,
aes(
x = log(x),
ymin = conf.low,
ymax = conf.high,
fill = as.numeric(Annual_Mean_temp),
group = group),
alpha = 0.15,
colour = NA,
inherit.aes = FALSE) +
geom_line(
data = pred,
aes(x = log(x),
y = predicted,
colour = as.numeric(Annual_Mean_temp),
group = group),
linewidth = 1.1)+
geom_point(alpha = 0.5) +
scale_colour_gradientn(
colours = c(
"#04111D",
"#445394",
"#9A98C0FF",
"#DABD61FF",
"#D95F30FF",
"#BE3428FF"),
values = c(0, 0.1, 0.35, 0.45, 0.65, 0.9, 1),
name = "Mean river \ntemperature\n(°C)") +
scale_fill_gradientn(
colours = c(
"#04111D",
"#445394",
"#9A98C0FF",
"#DABD61FF",
"#D95F30FF",
"#BE3428FF"),
values = c(0, 0.1, 0.35, 0.45, 0.65, 0.9, 1),
guide = "none") +
theme_bw() +
#xlim(-2.5, 0) +
Theme +
labs(
x = "ln mass- and temperature-\ncorrected metabolism ",
y = expression("Growth (mm day"^{-1}*")"))+xlab(expression(paste("Ln metabolism", " (mg O"[2]," h"^-1," g"^-"0.86"," K"^-"0.36",")")))+
theme(legend.position = c(0.16, 0.98),legend.justification = c(1, 1),legend.background = element_rect(fill = NA, colour = NA),
legend.key = element_rect(fill = NA, colour = NA))
plot_grid(
p1, p3,
ncol = 1,
rel_heights = c(1.5, 1),labels=c("a","b"),label_size = 16
)
ggsave("Plots/Growth vs Met and Temp2.tiff", width = 12, height = 12, units = "in", dpi = 200)
p1 = ggplot(
Fin,
aes(
log(MR_MT),
G_REcorr,
colour = Annual_Mean_temp))  +
geom_ribbon(
data = pred,
aes(
x = log(x),
ymin = conf.low,
ymax = conf.high,
fill = as.numeric(Annual_Mean_temp),
group = group),
alpha = 0.15,
colour = NA,
inherit.aes = FALSE) +
geom_line(
data = pred,
aes(x = log(x),
y = predicted,
colour = as.numeric(Annual_Mean_temp),
group = group),
linewidth = 1.1)+
geom_point(alpha = 0.5) +
scale_colour_gradientn(
colours = c(
"#04111D",
"#445394",
"#9A98C0FF",
"#DABD61FF",
"#D95F30FF",
"#BE3428FF"),
values = c(0, 0.1, 0.35, 0.45, 0.65, 0.9, 1),
name = "Mean river \ntemperature\n(°C)") +
scale_fill_gradientn(
colours = c(
"#04111D",
"#445394",
"#9A98C0FF",
"#DABD61FF",
"#D95F30FF",
"#BE3428FF"),
values = c(0, 0.1, 0.35, 0.45, 0.65, 0.9, 1),
guide = "none") +
theme_bw() +
#xlim(-2.5, 0) +
Theme +
labs(
x = "ln mass- and temperature-\ncorrected metabolism ",
y = expression("Growth (mm day"^{-1}*")"))+xlab(expression(paste("Ln metabolism", " (mg O"[2]," h"^-1," g"^-"0.86"," K"^-"0.36",")")))+
theme(legend.position = c(0.155, 0.98),legend.justification = c(1, 1),legend.background = element_rect(fill = NA, colour = NA),
legend.key = element_rect(fill = NA, colour = NA))
plot_grid(
p1, p3,
ncol = 1,
rel_heights = c(1.5, 1),labels=c("a","b"),label_size = 16
)
ggsave("Plots/Growth vs Met and Temp2.tiff", width = 12, height = 12, units = "in", dpi = 200)
